<?PHP
/****************************************/
/************* IdeaLAN LLC **************/
/* This is the main CONFIG file where   */
/* You need to specify database, and    */
/* other settings like lenght of the    */
/* password.                            */
/****************************************/
/****************************************/
/* SCRIPT: config.php        VER: 1.2  */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
/****************************************/

/* --------- FUNCTIONS --------- */
/*
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
*/

// GENERATE RANDOM PASSWORD.
function generatePassword () {
	global $passwordLengthMAX;
	// START WITH BLANK
	$password = "";

	// CHARACTERS USED TO GENERATE PASSWORD
	$possible = "0123456789abcdefghijklmnoprstuwxzyABCDEFGHIJKLMNOPRSTUWXYZQ9876543210"; 
    
	// ZERO THE COUNTER
	$i = 0; 
    
	// add random characters to $password until $length is reached
	while ($i < $passwordLengthMAX) { 

		// pick a random character from the possible ones
		$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
        
		// we don't want this character if it's already in the password
		if (!strstr($password, $char)) { 
      		$password .= $char;
      		$i++;
    	}

  }

  // DONE SEND PASSWORD
  return $password;

}

function md6decode($content,$lock) {
	for ($i = 1; $i <= $lock; $i++) {
    	$content = base64_decode($content);
	}
	
	return $content;
}

function format_phone($phone)
{
	$phone = preg_replace("/[^0-9]/", "", $phone);
 
	if(strlen($phone) == 7)
		return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
	elseif(strlen($phone) == 10)
		return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
	else
		return $phone;
}

/* ---------- MISC VARAIBLES ---------- */
ini_set("memory_limit","200M");
$timedate = time();

$info_ipaddr = $_SERVER["REMOTE_ADDR"];
$info_dns = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$info_current_url = basename($_SERVER["REQUEST_URI"]);

if(isset($_SERVER['HTTP_REFERER'])) {

   $info_reffered_url = $_SERVER['HTTP_REFERER'] . "";

} else {  $info_reffered_url = ""; }


$path_base = exec("pwd");
$path_find = "members";
$pos = strpos($path_base, $path_find);

if (empty($cFile)) {

		if ($pos == false ) { 
			if (isset($_SESSION["set_bpath"])) {
				$cFile = $_SESSION["set_bpath"] . "addons/config.ini"; 
			} else {
				$cFile = "./addons/config.ini";
			}

		} else {
			 $cFile = "../addons/config.ini"; 
		}

}

$fh = fopen($cFile, 'r');
$string = fread($fh, filesize($cFile));
fclose($fh);

$dstring = md6decode($string,2);

list($server,$username,$password,$database,$DBprefix) = explode("|", $dstring);


/* ---------- RANKS/GROUPS FOR USERS ---------- */
$ranks = array(
		1=>"User",
		2=>"Employee",
		3=>"Manager",
		4=>"Supervisor",
		5=>"ADMIN"
	      );
		  

/* ---------- OPTIONS OF THE ACP ---------- */

// !!!!!!!!!!!! THIS EVENTUALLY NEEDS TO BE MOVED IN TO THE SQL AND HAVE CONFIGURED WHEN INSTALLING THE SCRIPT.
// !!!!!!!!!!!! SOME OF THESE OPTIONS ARE NOT NEEDED ANYMORE AND CAN BE REMOVED.

$usernameLengthMIN = 4; 		// Sets the minium nubmer of characters in the username 
$usernameLengthMAX = 20;		// Sets the maxium number of characters in the username (max 20 chars!)
$passwordLengthMIN = 4; 		// Sets the minium nubmer of characters in the password 
$passwordLengthMAX = 10;			// Sets the maxium number of characters in the password (max 20 chars!)
$UseMailConfirm = true; 		// Only set this to false if your host does not support mail()
$AllowForgotPassword = true; 	// Only set this to false if your host does not support mail()
$UsernameValCharOnly = true; 	// If set on true the user can only use usernames with A to Z, a to z, 0 to 9
$passwordValCharOnly = true; 	// If set on true the user can only use passwords with A to Z, a to z, 0 to 9
$makeAdminOnlyActivate = false;	// Set this to true if you want to activate al accounts by hand throught the admin screen
$allowResend = true;			// Set this to false if you don't want the users to be able to have the activation code resend
			        			// Note that the form will be visible anyway and that if $UseMailConfirm = false if wont be send either
$TripodSupport = false;			// Only set this to true if you are hosted on tripod.


/* --------- ERROR FILE -------- */
include "errors.php";


/* -------------------------------------------- DO NOT EDIT BELOW -------------------------------------------- */

/* ---------- CREATE DATABASE AND CONNECT-------- */
// THIS CAN ALSO BE SPECIFIED IN THE INSTALL FILE.

// ADD "_" to the prefix if scpecified.
if ($DBprefix != ""){$DBprefix = $DBprefix . "_";}

// Code to make sure that the $database was filled and code to connect to the database
if ($database == ""){
	// Check it the database exist
	$query = "USE acp";
	// If this query is succesfull, and returns a 1. this means that the database members can be used.
	// If it returns 'null' then it doesn't exist.
	if (mysql_query($query) == null){
		//if it doesn't exist we create it and connect to it.
		$query = "CREATE DATABASE acp";
		$result = mysql_query($query);
		
		if ($result == 1){
			
			$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql); 
			mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);
			
		} else {
		
			echo "Error while creating database (Errornumber ". mysql_errno() .": \"". mysql_error() ."\")<br>";
		
		}
		
	} else {
	
		// It already exist so we will connect to it.
		$database= "acp";
		$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql); 
		mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);		
	}
	
} else {

	// connect or show an error.
	$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql); 
	mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);
}

// LETS GET CONFIGURATION INFO
$get_config_info = "SELECT * FROM `".$DBprefix."settings` WHERE `conf_id` = '1' LIMIT 1";
$execute_config_info = mysql_query($get_config_info); 

if ($config = mysql_fetch_array($execute_config_info)) {

	$conf_weburl = $config["conf_weburl"];
	$conf_acpurl = $config["conf_acpurl"];
	$conf_bpath = $config["conf_bpath"];
	$conf_apath = $config["conf_apath"];
	$conf_mpath = $config["conf_mpath"];
	$conf_tpath = $config["conf_tpath"];
	$conf_passmin = $config["conf_passmin"];
	$conf_passmax = $config["conf_passmax"];
	$conf_timeout = $config["conf_timeout"];
	$conf_alin = $config["conf_alin"];
	$conf_alou = $config["conf_alou"];
	$conf_shosid = $config["conf_shosid"];
}

?>
